<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller\game;

/**
 * 微派公众号支付
 *
 * @author SoChishun
 */
class WiipaywxController extends \app\index\controller\BaseController {

    public function indexAction() {
        if ($this->loginInfo['currency'] < 1) {
            $this->error('您的竞猜币不足，请充值!', url('finance.recharge/recharge'));
        }
        return view();
    }

    // 微派wPay
    public function dowiipaywxAction() {
        if ($this->loginInfo['currency'] < 1) {
            return ['success' => false, 'message' => '您的竞猜币不足，请充值!'];
        }
        $request = \think\Request::instance();
        $request->only(['da', 'xiao', 'dan', 'shuang']);
        $data = $request->post();
        $game = new \app\index\model\GameRecordModel();
        $result = $game->saveRecord($data, $this->loginInfo['id']);
        if (!$result['success']) {
            return ['success' => false, 'message' => $result['message']];
        }

        $out_trade_no =$result['message']['out_trade_no'];
        $amount = $data['amount'];

        $product = '充值' . $amount . '元';
        $url = 'http://api.xiipay.com/Pay';
        $params = [
            'amount' => $amount, // 金额（单位为分），非空
            'backurl' => url('finance.recharge/history'), // 支付结束跳转地址，非空
            'desc' => $product, // 商品描述，200 位以内，可以为空
            'extra' => $out_trade_no, // 透传参数，32 位以内，可以为空
            'merch' => 0, // 商户id，平台分配
            'notifyurl' => url('finance.wiipaywx/notify'), // 同步地址，非空
            'product' => $product, // 商品名称，64 位以内，非空
            'type' => 1, // 支付场景，固定填1
        ];
        ksort($params);
        $strparams = http_build_query($params);
        $sign = md5($strparams);
        $url = $url . '?' . $strparams . '&sign=' . $sign;
        /*
         * MD5.md5("amount="+amount+"&backurl="+backurl+"&desc="+desc+"&extra="+extra+"&merch
          ="+merch+"&notifyurl="+notifyurl+"&product="+product+"&type="+type+"&key="+mch_secret);
         */
        redirect($url)->send();
    }
}
